<template>
  <div>
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-card>
      <!-- 搜索框区域 -->
      <el-row :gutter="20">
        <el-col :span="8">
          <div>
            <el-input
              clearable
              placeholder="请输入内容"
              v-model="queryInfo.query"
              @clear="getOrdersList"
            >
              <el-button
                slot="append"
                icon="el-icon-search"
                @click="getOrdersList"
              ></el-button>
            </el-input>
          </div>
        </el-col>
      </el-row>

      <!-- 商品列表表格 -->
      <el-row>
        <el-table :data="ordersList" border stripe style="width: 100%">
          <el-table-column type="index"> </el-table-column>
          <el-table-column prop="order_number" label="订单编号">
          </el-table-column>
          <el-table-column prop="order_price" width="120" label="订单价格(元)">
          </el-table-column>
          <el-table-column prop="pay_status" width="130" label="是否付款">
            <template slot-scope="scope">
              <el-tag type="danger" v-if="scope.row.pay_status === '0'"
                >未付款</el-tag
              >
              <el-tag type="success" v-else>已付款</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="is_send" width="100" label="是否发货">
          </el-table-column>
          <el-table-column prop="create_time" label="下单时间">
            <template slot-scope="scope">
              {{ scope.row.create_time | format('yyyy-MM-dd hh:mm:ss') }}
            </template>
          </el-table-column>
          <el-table-column width="220" label="操作">
            <!-- 操作模板 -->
            <template slot-scope="scope">
              <!-- 修改按钮 -->
              <el-tooltip
                effect="dark"
                content="修改"
                placement="top-start"
                :enterable="false"
              >
                <el-button
                  type="primary"
                  @click="showEditDialog"
                  icon="el-icon-edit"
                >
                </el-button>
              </el-tooltip>

              <!--查看物流按钮 -->
              <el-tooltip
                effect="dark"
                content="查看物流"
                placement="top-start"
                :enterable="false"
              >
                <el-button
                  type="success"
                  @click="showProgressBox"
                  icon="el-icon-location"
                >
                </el-button>
              </el-tooltip>
            </template>
          </el-table-column>
        </el-table>
      </el-row>

      <!-- 分页功能 -->
      <el-row>
        <div class="block">
          <el-pagination
            background
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
            :current-page="queryInfo.pagenum"
            :page-sizes="[6, 8, 10, 12]"
            :page-size="queryInfo.pagesize"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
          ></el-pagination>
        </div>
      </el-row>
    </el-card>

    <!-- 修改地址对话框 -->
    <el-dialog
      title="修改地址"
      @close="dialogClose"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form
        :model="addressForm"
        ref="addressFormRef"
        :rules="addressFormRules"
        label-width="100px"
      >
        <el-form-item label="省市区/县" prop="address">
          <!--级联选择框 options用来指定数据源  
          clearable 是否可以清空-->
          <el-cascader
            clearable
            v-model="addressForm.address"
            :options="cityOptions"
            :props="{ expandTrigger: 'hover' }"
          >
          </el-cascader>
        </el-form-item>

        <el-form-item label="详细地址" prop="detailAddress">
          <el-input v-model="addressForm.detailAddress"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="editDialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>

    <el-dialog
      title="物流查询"
      :visible.sync="progressDialogVisible"
      width="50%"
    >
      <!-- Timeline 时间线 -->
      <el-timeline>
        <el-timeline-item
          v-for="(item, index) in progressInfo"
          :key="index"
          :timestamp="item.time"
        >
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityOptions from './city_data2017_element.js'
export default {
  data() {
    return {
      //订单列表
      ordersList: [],
      queryInfo: {
        query: '',
        pagenum: 1, //当前页码
        pagesize: 6 //每页显示条数
      },
      total: 0, //数据总条数
      //控制修改收货地址对话框的显示与隐藏
      editDialogVisible: false,
      addressForm: {
        address: [],
        detailAddress: ''
      },
      addressFormRules: {
        address: [
          { required: true, message: '请输入省市区/县', trigger: 'blur' }
        ],
        detailAddress: [
          { required: true, message: '请输入详细地址', trigger: 'blur' }
        ]
      },
      cityOptions: cityOptions,
      //控制查看物流信息对话框的显示与隐藏
      progressDialogVisible: false,
      progressInfo: []
    }
  },
  created() {
    this.getOrdersList()
  },
  methods: {
    async getOrdersList() {
      const { data: response } = await this.$axios.get('orders', {
        params: this.queryInfo
      })
      //   console.log(response)
      if (response.meta.status !== 200)
        return this.$message.error('获取订单列表失败')
      this.ordersList = response.data.goods
      this.total = response.data.total
      return this.$message.success(response.meta.msg)
    },
    //监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      //   console.log(`每页 ${newSize} 条`)
      this.queryInfo.pagesize = newSize
      this.getOrdersList()()
    },
    //监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      //   console.log(`当前页: ${newPage}`)
      this.queryInfo.pagenum = newPage
      this.getOrdersList()
    },
    showEditDialog() {
      this.editDialogVisible = true
    },
    //监听修改地址对话框关闭的事件
    dialogClose() {
      this.$refs.addressFormRef.resetFields()
      //   console.log(this)
    },
    async showProgressBox() {
      const { data: res } = await this.$axios.get('/kuaidi/804909574412544580')

      if (res.meta.status !== 200) {
        return this.$message.error('获取物流进度失败！')
      }

      this.progressInfo = res.data

      this.progressDialogVisible = true
      //   console.log(this.progressInfo)
    }
  }
}
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
}
.block {
  display: flex;
  justify-content: center;
}
.el-cascader {
  width: 100%;
}
</style>
